package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.wcfsy.wechat.wx.entity.PaymentFlow;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.PaymentFlowDao;
import com.wcfsy.wechat.wx.util.StringUtil;
import com.wcfsy.wechat.wx.util.excel.ExcelView;

@Controller
@RequestMapping("/cms/orderNumber")
public class OrderNumberController {

	private  Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private PaymentFlowDao paymentFlowDao;

	@RequestMapping({ "/view/page" })
	public String page() {
		return "orderNumber/orderNumber_list";
	}

	@RequestMapping({ "/view" })
	@ResponseBody
	public IPage<PaymentFlow> view(int page, int rows,
			String minDate,String maxDate,
			String orderNumber,String productName,String userName,
			String mobile,String state) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(minDate)){
			paramMap.put("minDate", minDate);
		}
		if(StringUtils.isNotEmpty(maxDate)){
			paramMap.put("maxDate", maxDate);
		}
		if(StringUtils.isNotEmpty(orderNumber)){
			paramMap.put("orderNumber", orderNumber);
		}
		if(StringUtils.isNotEmpty(productName)){
			paramMap.put("productName", productName);
		}
		if(StringUtils.isNotEmpty(userName)){
			paramMap.put("userName", StringUtil.encode(userName));
		}
		if(StringUtils.isNotEmpty(state)){
			paramMap.put("state", state);
		}
		if(StringUtils.isNotEmpty(mobile)){
			paramMap.put("mobile", mobile);
		}
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		IPage<PaymentFlow> page1 = new Page<PaymentFlow>(paymentFlowDao.findByParam(paramMap),
				paymentFlowDao.count(paramMap), page, rows);
		logger.info("liebiao  = " + JSON.toJSONString(page1));
		List<PaymentFlow> list = page1.getData();
		for (int i = 0; i < list.size() ; i++) {
			if(StringUtils.isNotEmpty(page1.getData().get(i).getUserName())){
				page1.getData().get(i).setUserName(StringUtil.decode(page1.getData().get(i).getUserName()));
			}
			if("ok".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("支付成功");
			}else if("cancel".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("取消");
			}else if("fail".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("失败");
			}else if("notpaid".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("未支付");
			}
		}
		return page1;
	}

	/**
	 * 到处excel 表格
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getOrderNumber")
	public ModelAndView getOrderNumber(
			String minDate,String maxDate,
			String orderNumber,String productName,String userName,
			String mobile,String state, ModelMap modelMap) {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(minDate)){
			paramMap.put("minDate", minDate);
		}
		if(StringUtils.isNotEmpty(maxDate)){
			paramMap.put("maxDate", maxDate);
		}
		if(StringUtils.isNotEmpty(orderNumber)){
			paramMap.put("orderNumber", orderNumber);
		}
		if(StringUtils.isNotEmpty(productName)){
			paramMap.put("productName", productName);
		}
		if(StringUtils.isNotEmpty(userName)){
			paramMap.put("userName", StringUtil.encode(userName));
		}
		if(StringUtils.isNotEmpty(state)){
			paramMap.put("state", state);
		}
		if(StringUtils.isNotEmpty(mobile)){
			paramMap.put("mobile", mobile);
		}
		paramMap.put("index", 1);
		paramMap.put("size", 10000);
		paramMap.put("limit", "limit");
		IPage<PaymentFlow> page1 = new Page<PaymentFlow>(paymentFlowDao.findByParam(paramMap),
				paymentFlowDao.count(paramMap), 1, 10000);
		List<PaymentFlow> list = page1.getData();
		for (int i = 0; i < list.size() ; i++) {
			if(StringUtils.isNotEmpty(page1.getData().get(i).getUserName())){
				page1.getData().get(i).setUserName(StringUtil.decode(page1.getData().get(i).getUserName()));
			}
			if("ok".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("支付成功");
			}else if("cancel".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("取消");
			}else if("fail".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("失败");
			}else if("notpaid".equals(page1.getData().get(i).getState())){
				page1.getData().get(i).setState("未支付");
			}
		}
		if (page1 != null) {
			return outExcel(page1.getData(), modelMap);
		}
		return null;
	}
	
	private ModelAndView outExcel(List<PaymentFlow> wechats,
			ModelMap modelMap) {
		if (wechats.isEmpty() && wechats.size() < 0) {
			return null;
		}
		Map<String, String> headerMap = new LinkedHashMap<String, String>();
		headerMap.put("orderNumber", "订单号");
		headerMap.put("userName", "微信昵称");
		headerMap.put("mobile", "手机号");
		headerMap.put("productName", "产品名称");
		headerMap.put("memberName", "会员名称");
		headerMap.put("price", "价格");
		headerMap.put("createDate", "订单时间");
		headerMap.put("state", "状态");
		headerMap.put("discount", "折扣");
		headerMap.put("realPrice", "真实价格");
		String dateStr = DateUtil.formatDate(new Date(), "yyyyMMddHHMMSS");
		String fileName = dateStr + "-兑奖记录详情";
		ExcelView excelView = new ExcelView(wechats, headerMap, fileName);
		return new ModelAndView(excelView, modelMap);
	}
	
}
